<?php
class retrieve_page extends retrieve_base{

	private $module_display_options 	= array(
		array("PRESENTATION_SEARCH",	LOCALE_PAGE_DISPLAY_SEARCH_CHANNEL),
		array("PRESENTATION_DISPLAY",	LOCALE_PAGE_DISPLAY_AUTO_CHANNEL),
		array("PRESENTATION_LATEST",	LOCALE_PAGE_DISPLAY_NEWEST_CHANNEL)
	);

	function getDigitalDesktop($parameters){
		$debug = $this->debugit(false, $parameters);
		$grp_info = $this->check_parameters($_SESSION,"SESSION_GROUP",Array());
		$max_grps = count($grp_info);
		$user   = $_SESSION["SESSION_USER_IDENTIFIER"];
		$access_list = "";
		$access_array = array();
		$ALL=0;
		$open = 0;
		$userRestricted = 0;
		for($i=0;$i < $max_grps; $i++){
			$access = $grp_info[$i]["ACCESS"];
			$max = count($access);
			for($index=0;$index<$max;$index++){
				if ((substr($access[$index],0,strlen($this->module_command))==$this->module_command)||($access[$index]=="ALL")){
					$open=1;
				}
			}
		}
		$session_management_access	= $this->check_parameters($_SESSION, "SESSION_MANAGEMENT_ACCESS", Array());
		$session_man_access="";
		for($index=0,$max=count($session_management_access);$index<$max;$index++){
			$userRestricted=1;
			if ($index>0){
				$session_man_access .= ",";
			}
			$session_man_access .= " ".$session_management_access[$index];
		}
		$join1 ="";
		$join2 ="";
		$where = "";
		if ($userRestricted){
			$join1  = " inner join menu_access_to_page on (page_trans_data.trans_identifier = menu_access_to_page.trans_identifier and page_trans_data.trans_client = menu_access_to_page.client_identifier) ";
			$join1 .= " left outer join relate_user_menu on (relate_user_menu.menu_identifier = menu_access_to_page.menu_identifier and relate_user_menu.user_identifier = page_trans_data.trans_doc_lock_to_user) ";
			$join2  = " inner join menu_access_to_page on (page_trans_data.trans_identifier = menu_access_to_page.trans_identifier and page_trans_data.trans_client = menu_access_to_page.client_identifier) ";
			$join2 .= " left outer join relate_user_menu on (relate_user_menu.menu_identifier = menu_access_to_page.menu_identifier) ";
			$where = " and relate_user_menu.menu_identifier in ($session_man_access)";
			$link_user = $user;
		} else {
			$link_user = -1;
		}
		if ($open==1){
			if (($this->parent->server[LICENCE_TYPE]==ECMS)||($this->parent->server[LICENCE_TYPE]==MECM)){
				if ($this->module_admin_access){
					$out="<cmd label=\"".LOCALE_LIST."\">PAGE_LIST</cmd>";
				}
				if($this->author_access){
					/**
					* has this user checked out any documents
					*/
					$sql    = "select * from page_trans_data inner join available_languages on trans_language = language_code $join1 
					where trans_client =$this->client_identifier and trans_current_working_version=1 and trans_doc_lock_to_user=$user $where";
					if ($debug) print "<p><strong>:: ".__FILE__." @ ".__LINE__." ::</strong><br/>$sql</p>";
					$result = $this->db->query($sql);
					$out .= "<cmd label=\"".ADD_NEW."\">PAGE_ADD</cmd>";
					$out .= "<text><![CDATA[";
					$num = $this->db->num_rows($result);
					if ($num>0){
						$out .= "<a href='admin/index.php?command=PAGE_LIST&amp;status_filter=1&amp;user=$link_user&amp;lock=1'>";
						$out .= "Pages in checked out/draft ($num)" ;
						$out .= "</a>";
					} else {
						$out .= "Pages in checked out/draft (0)";
					}
					$out .= "]]></text>";
					if ($this->parent->server[LICENCE_TYPE]==ECMS){
						/**
						* any documents to review ???
						*/
						$now_csv = date("Y,m,d,H,i,s");
						$date_list = split(",",$now_csv);
						if ($date_list[1]==12){
							$date_list[1]=1;
							$date_list[0]++;
						} else {
							$date_list[1]++;
						}
						$now = $date_list[0]."/".$date_list[1]."/".$date_list[2]." ".$date_list[3].":".$date_list[4].":".$date_list[5];
						// should not need 'trans_current_working_version=1 and ' in sql
						$sql    = "select * from page_trans_data 
									inner join available_languages on trans_language = language_code 
									$join2 
									where 
									trans_current_working_version=1 and trans_client =$this->client_identifier and trans_published_version=1 and (trans_date_review <= '$now' and trans_date_review != '0000/00/00 00:00:00') $where";
						if ($debug) print "<p><strong>:: ".__FILE__." @ ".__LINE__." ::</strong><br/>$sql</p>";
						$result = $this->db->query($sql);
						$out .= "<text><![CDATA[";
						$num = $this->db->num_rows($result);
						if ($num>0){
							$out .= "<a href='admin/index.php?command=PAGE_LIST&amp;filter=review'&amp;lock=1>";
							$out .= "Pages requiring review ($num)";
							$out .= "</a>";
						} else {
							$out .= "Pages requiring review (0)";
						}
						$out .= "]]></text>";
					}
				}
				if ($this->parent->server[LICENCE_TYPE]==ECMS){
					if($this->approver_access){
						/**
						* any documents to approve
						*/
						$sql    = "select * from page_trans_data inner join available_languages on trans_language = language_code $join2 where trans_client =$this->client_identifier and trans_current_working_version=1 and trans_doc_status=2 $where";
						if ($debug) print "<p><strong>:: ".__FILE__." @ ".__LINE__." ::</strong><br/>$sql</p>";
						$result = $this->db->query($sql);
						$out .= "<text><![CDATA[";
						$num = $this->db->num_rows($result);
						if ($num>0){
							$out .= "<a href='admin/index.php?command=PAGE_LIST&amp;status_filter=2&amp;lock=1'>";
							$out .= "Pages awaiting your approval ($num)";
							$out .= "</a>";
						} else {
							$out .= "Pages awaiting your approval (0)";
						}
						$out .= "]]></text>";
					}
				}
				if($this->publisher_access){
					$sql    = "select * from page_trans_data inner join available_languages on trans_language = language_code $join2 where trans_client =$this->client_identifier and trans_current_working_version=1 and trans_doc_status=3 $where";
					if ($debug) print "<p><strong>:: ".__FILE__." @ ".__LINE__." ::</strong><br/>$sql</p>";
					$result = $this->db->query($sql);
					$out .= "<text><![CDATA[";
					$num = $this->db->num_rows($result);
					if ($num>0){
						$out .= "<a href='admin/index.php?command=PAGE_LIST&amp;status_filter=3&amp;lock=1'>";
						$out .= "Pages awaiting publisher approval ($num)";
						$out .= "</a>";
					} else {
						$out .= "Pages awaiting publisher approval (0)";
					}
					$out .= "]]></text>";

				}
				if ($this->parent->server[LICENCE_TYPE]==ECMS){
					/**
					* any documents to review ???
					*/
					$now = $this->libertasGetDate("Y/m/d H:i:s");
					// should not need 'trans_current_working_version=1 and ' in sql
					$sql    = "select * from page_trans_data 
								inner join available_languages on trans_language = language_code 
								$join2 
								where 
								trans_current_working_version=1 and trans_client =$this->client_identifier and trans_published_version=1 and (trans_date_available > '$now') $where";
					if ($debug) print "<p><strong>:: ".__FILE__." @ ".__LINE__." ::</strong><br/>$sql</p>";
					$result = $this->db->query($sql);
					$out .= "<text><![CDATA[";
					$num = $this->db->num_rows($result);
					if ($num>0){
						$out .= "<a href='admin/index.php?command=PAGE_LIST&amp;filter=available&amp;lock=1'>";
						$out .= "Pages with date embargo ($num)";
						$out .= "</a>";
					} else {
						$out .= "Pages with date embargo (0)";
					}
					$out .= "]]></text>";
				}
				return "<module name=\"pages\" label=\"".MANAGEMENT_PAGE."\" display=\"my_workspace\">".$out."1</module>";
			} else {
				$user   = $_SESSION["SESSION_USER_IDENTIFIER"];
				if ($this->module_admin_access){
					$out="<cmd label=\"".LOCALE_LIST."\">PAGE_LIST</cmd>";
				}
				$sql    = "select * from page_trans_data inner join available_languages on trans_language = language_code where trans_client =$this->client_identifier and trans_current_working_version=1 and trans_doc_status = 1";
				if ($debug) print "<p><strong>:: ".__FILE__." @ ".__LINE__." ::</strong><br/>$sql</p>";
				$result = $this->db->query($sql);
				$out.="<cmd label=\"".ADD_NEW."\">PAGE_ADD</cmd>";
				$out .= "<text><![CDATA[";
					$num = $this->db->num_rows($result);
					if ($num>0){
						$out .= "<a href='admin/index.php?command=PAGE_LIST&amp;status_filter=1&amp;user=$link_user'>";
						$out .= "Pages in checked out/draft ($num)" ;
						$out .= "</a>";
					} else {
						$out .= "Pages in checked out/draft (0)";
					}
					$out .= "]]></text>";
				return "<module name=\"pages\" label=\"".MANAGEMENT_PAGE."\" display=\"my_workspace\">".$out."</module>";
			}
		}else{
			return "";
		}
	}
	
		/**
	* function produces abstract structure of modules db structure
	*
    * used to generate the table structure required abstract function that passes an abstract
	* representation of the desired table structure to the proper database module which will
	* interpet the abstract and convert it into a valid SQL Create table structure.
	*
	* @return Array list of abstract table definitions for this module
	*/
	function create_table(){
		$tables = array();

		/**
		* Table structure for table 'pages'
		*/
		
		$fields = array(
			array("page_identifier"				,"unsigned integer"			,"NOT NULL"	,"auto_increment","key"),
			array("page_is_title_doc"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("page_client"					,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("page_overall_status"			,"unsigned small integer"	,"NOT NULL"	,"default '0'"),
			array("page_date_creation"			,"datetime"					,"NOT NULL"	,"default ''"),
			array("page_created_by_user"		,"unsigned integer"			,"NOT NULL"	,"default '0'","key"),
			array("page_admin_discussion"		,"unsigned small integer"	,"NOT NULL"	,"default '0'"),
			array("page_web_discussion"			,"unsigned small integer"	,"NOT NULL"	,"default '0'")
		);
		
		$primary ="page_identifier";
		$tables[count($tables)] = array("page_data", $fields, $primary);

		/**
		* Table structure for the translations and version control of page content
		*/
		$fields = array(
		/**
		* Basic connectivity fields
		* the page it belongs to 
		* the client that owns this data
		* the language that the document is written in default 'english' if not supplied
		*/
		array("trans_identifier"			,"unsigned integer"	,"NOT NULL"	,"auto_increment","key"),
		array("trans_page"					,"unsigned integer"	,"NOT NULL"	,"default '0'","key"),
		array("trans_client"				,"unsigned integer"	,"NOT NULL"	,"default '0'"),
		array("trans_language"				,"varchar(10)"		,"NOT NULL"	,"default 'en'"),
		/**
		* editable content
		*/
		array("trans_title"					,"varchar(255)"		,"NOT NULL"	,"default ''"),
//		array("trans_summary"				,"text"				,"NOT NULL"	,"default ''"),
//		array("trans_body"					,"text"				,"NOT NULL"	,"default ''"),
		/**
		* is this document published to the site yet
		* the version numbers of this document Major . Minor
		* Major gets updated when fully approved,
		* Minor gets updated when sent for approval,
		* translation locked to one user when in edit mode.
		* trans_current_working_version on saving a new versionthis is set to 1 previous version 
		* set to 0
		*/
		array("trans_doc_status"				,"small integer"	,"NOT NULL"	,"default '1'","key"),
		array("trans_doc_version_major"			,"unsigned integer"	,"NOT NULL"	,"default '0'"),
		array("trans_doc_version_minor"			,"unsigned integer"	,"NOT NULL"	,"default '1'"),
		array("trans_doc_lock_to_user"			,"unsigned integer"	,"NOT NULL"	,"default '0'","key"),
		array("trans_doc_author_identifier"		,"unsigned integer"	,"NOT NULL"	,"default '0'","key"),
		array("trans_current_working_version"	,"small integer"	,"NOT NULL"	,"default '0'"),

		/**
		* Translation Date information
		-
		* creation date  :: is the creation date of that language version of the page and will not
		* 				    change through versions but will change for each new language used
		* modified date  :: is the date the translation was created.
		* review date    :: is the date that this document has to be reviewed.
		* publish date	 :: is the date that this version was published to the site.
		* remove date    :: is the date that this document should be automatically removed from the 
		-				   	site.
		* available date :: is the date that this document will be avaliable onthe site can be equal 
		*                   to or greater than the publish date 
		*/
		array("trans_date_creation"			,"datetime"			,""			,"default ''"),
		array("trans_date_modified"			,"datetime"			,""			,"default ''"),
		array("trans_date_review"			,"datetime"			,""			,"default ''"),
		array("trans_date_publish"			,"datetime"			,""			,"default ''"),
		array("trans_date_remove"			,"datetime"			,""			,"default ''"),
		array("trans_date_available"		,"datetime"			,""			,"default ''"),
		/**
		* dublin core meta data and extra generated by us
		-
		* best bets :: the ability to store search phrases for a document to help in search results
		* DC_*		:: dublin core metadata.
		*/
		array("trans_best_bets"				,"text"				,"NOT NULL"	,"default ''"),
		array("trans_dc_keywords"			,"text"				,"NOT NULL"	,"default ''"),
		array("trans_dc_alt_title"			,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_audience"			,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_contributor"		,"text"				,"NULL"		,"default ''"),
		array("trans_dc_creator"			,"text"				,"NULL"		,"default ''"),
		array("trans_dc_coverage_place"		,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_coverage_postcode"	,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_coverage_time"		,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_doc_type"			,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_publisher"			,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_rights"				,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_rights_copyright"	,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_source"				,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_subject_category"	,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_subject_keywords"	,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_subject_programme"	,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_subject_project"	,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_dc_url"				,"varchar(255)"		,"NULL"		,"default ''"),
		array("trans_published_version"		,"small integer"	,"NULL"		,"default '0'")
		);
		
		$primary ="trans_identifier";
		$tables[count($tables)] = array("page_trans_data", $fields, $primary);
		/**
		* Table structure for table 'page_status'
		*/
		$fields = array(
		array("page_status_identifier"		,"unsigned integer"	,"NOT NULL"	,"auto_increment","key"),
		array("page_status_label"			,"varchar(25)"	,"NOT NULL"	,"default ''"),
		array("page_status_constant"		,"varchar(25)"	,"NOT NULL"	,"default ''")
		);
		
		/**
		* insert page_status data
		*/
		
		$data = array(
		'INSERT INTO page_status (page_status_label, page_status_constant) VALUES("LOCALE_AUTHORING", "__STATUS_AUTHOR__");',
		'INSERT INTO page_status (page_status_label, page_status_constant) VALUES("LOCALE_READY", "__STATUS_READY__");',
		'INSERT INTO page_status (page_status_label, page_status_constant) VALUES("LOCALE_UNPUBLISHED", "__STATUS_APPROVED__");',
		'INSERT INTO page_status (page_status_label, page_status_constant) VALUES("LOCALE_PUBLISHED", "__STATUS_PUBLISHED__");',
		'INSERT INTO page_status (page_status_label, page_status_constant) VALUES("LOCALE_ARCHIVED", "__STATUS_ARCHIVED__");',
		'INSERT INTO page_status (page_status_label, page_status_constant) VALUES("LOCALE_REMOVED", "__STATUS_REMOVED__");'
		);
		$primary ="page_status_identifier";
		
		$tables[count($tables)] = array("page_status", $fields, $primary, $data);

		/**
		* Table structure for table 'page_search_keys'
		*/
		
		$fields = array(
		array("search_keyword"			,"varchar(50)"		,"NOT NULL"	,"default ''"),
		array("search_counter"			,"unsigned integer"	,"NOT NULL"	,"default '0'"),
		array("search_client"			,"unsigned integer"	,"NOT NULL"	,"default ''")
		);
		
		$primary ="";
		$tables[count($tables)] = array("page_search_keys", $fields, $primary);
		/**
		* Table structure for table 'page_latest'
		*/
		
		$fields = array(
			array("page_latest_identifier"	,"unsigned integer"	,"NOT NULL"	,"auto_increment","key"),
			array("page_latest_label"		,"varchar(255)"		,"NOT NULL"	,"default '0'"),
			array("page_latest_type"		,"unsigned integer"	,"NOT NULL"	,"default ''"),
			array("page_latest_client"		,"unsigned integer"	,"NOT NULL"	,"default ''")
		);
		
		$primary ="";
		$tables[count($tables)] = array("page_latest", $fields, $primary);
		/**
		* Table structure for table 'page_latest_locations'
		*/
		
		$fields = array(
			array("page_latest_locations_identifier"	,"unsigned integer"	,"NOT NULL"	,"auto_increment","key"),
			array("page_latest_locations_client"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("page_latest_locations_menu"			,"unsigned integer"	,"NOT NULL"	,"default ''")
		);
		
		$primary ="";
		$tables[count($tables)] = array("page_latest_locations", $fields, $primary);

		return $tables;
	}
	
}
?>